import {
  HostBinding,
  Component,
  OnInit,
  ViewChild,
  Input,
  Renderer2,
  forwardRef,
  Injector,
  Inject,
  DoCheck
} from "@angular/core";
import {
  Observable,
  Subject,
  ReplaySubject,
  from,
  of,
  range,
  BehaviorSubject
} from "rxjs";
import { map, catchError } from "rxjs/operators";
import { Router } from "@angular/router";
import {
  AsyncValidator,
  NG_ASYNC_VALIDATORS,
  ValidationErrors,
  AbstractControl,
  FormBuilder,
  FormGroup,
  Validators,
  ControlValueAccessor,
  NG_VALUE_ACCESSOR,
  FormControl
} from "@angular/forms";
import { Store, select } from "@ngrx/store";
import { Actions } from "@ngrx/effects";
import * as fromUser from "@/user/store/reducers/index";
import * as fromRoot from "@/reducers/index";
import { UserActions } from "@/user/store/actions";
import { ILoginRequest } from "@/user/store/models/user";

import { PLATFORM_ID, APP_ID } from "@angular/core";
import { isPlatformServer } from "@angular/common";
import { LocaleService, TranslationService, Language } from "angular-l10n";

import { ActivatedRoute } from "@angular/router";

type IOnTab = "send" | "reset"; // 发送验证码 | 重置密码

@Component({
  selector: "app-forgot2",
  templateUrl: "./forgot2.component.html",
  styleUrls: ["./forgot2.component.scss"]
})
export class Forgot2Component implements OnInit, DoCheck {
  @HostBinding("style.height")
  public height = "100%";
  @HostBinding("style.display")
  public display = "flex";
  @HostBinding("style.align-items")
  public alignItems = "center";

  @Language() lang: string;

  user$ = this.store.pipe(select(fromRoot.user));

  validateForm2: FormGroup;

  hide = true;

  isServer: boolean;

  onTab: IOnTab = "send";

  req: ILoginRequest = {
    username: "",
    password: "",
    code: "",
    verify: {}
  };

  constructor(
    updates$: Actions,
    @Inject(PLATFORM_ID) private platformId: Object,
    private fb: FormBuilder,
    private renderer: Renderer2,
    @Inject("StoreService") private storeService,
    @Inject("RegService") private regService,
    @Inject("UtilsService") private utilsService,
    @Inject("UserService") private userService,
    @Inject("MsgService") private msgService,
    private activatedRoute: ActivatedRoute,
    @Inject("HasUsernameValidatorService") private hasUsernameValidatorService,
    private router: Router,
    private store: Store<fromUser.State | fromRoot.State>
  ) {
    this.isServer = isPlatformServer(this.platformId);
  }

  ngOnInit(): void {
    this.validateForm2 = this.fb.group({
      password: new FormControl(null, {
        validators: [Validators.required],
        updateOn: "change"
      })
    });
    this.addListen();
  }

  ngDoCheck() {}

  addListen() {
    this.activatedRoute.queryParams.subscribe(e => {
      try {
        this.req = JSON.parse(e.req);
      } catch (e) {
        this.router.navigate(["share/hack"]);
      }
    });
    this.validateForm2.valueChanges.subscribe(e => {
      this.req.password = e.password;
    });
  }

  async onSubmit() {
    // this.store.dispatch(new UserActions.Login({ req: this.req, cpt: this }));
  }
}
